// pages/partner_code/partner_code.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: {},
    qr_code: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    wx.getUserInfo({
      withCredentials: true,
      success: function (res) {
        that.setData({
          userinfo: res.userInfo
        })
      }
    })
    app.getSessionId().then(function () {
      wx.request({
        url: app.globalData.data_url + '/GPD/getQrcode',
        data: {
          session_id: app.globalData.sessionId,
          path: 'pages/distribution/distribution'
        },
        success: function (res) {
          wx.hideLoading();
          that.setData({
            qr_code: res.data.qrcodeurl
          })
        }
      })
    })
  },
  saveimg: function () {
    var that = this
    wx.showActionSheet({
      itemList: ['保存图片到相册'],
      success: function (res) {
        if (res.tapIndex == 0) {
          wx.downloadFile({
            url: that.data.qr_code,
            success: function (res) {
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success: function (res) {
                  if (res.errMsg == 'saveImageToPhotosAlbum:ok') {
                    wx.showToast({
                      title: '保存成功',
                    })
                  }
                },
                fail: function (res) {
                  if (res.errMsg == "saveImageToPhotosAlbum:fail auth deny" || res.errMsg == "saveImageToPhotosAlbum:fail:auth denied") {
                    wx.openSetting()
                  } else {
                    wx.showModal({
                      title: 'tip',
                      content: res.errMsg,
                    })
                  }
                }
              })
            }
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})